var GroupUsers = function () {
	var fnAddCallback= function(data){//Form表单提交信息
		$('#groupUserModal').modal('hide');
	};
    return {
    	datatable:"",
        init: function () {
        	this.initTree();
        	this.search();
        	this.setListener();
        },
        initTree:function(){
        	var doClick = function() {
    			$.ajax({
                    "type": "GET",
                    "cache": false,
                    "async " : false,
                    "url": ctx+"/group/"+UITree.selectedId+"/gettype?now=" + new Date().getTime(), 
                    "success": function(data){
           				$("#treeType").val(data.level);
           				$("#parentIds").val(data.parentIds);
           				$("#checkbox-parent").attr("checked", false);
                    }
                });
    			GroupUsers.reSearch();
    	   	};
    		UITree.initTree({
    		   	"url" 		: ctx+"/group/getTrees?now="+new Date().getTime(),
    		   	"id"	 	: "tree_2",
    		   	"checkbox"	: false,
    		   	"click"		: doClick,
    		   	"disable"	: false
    	   	});
        },
        search:function(){
        	if (!jQuery().dataTable) {
                return;
            }
            var groupUserListTable  = $("#groupUserListTable").dataTable({
                "sAjaxSource": ctx+"/user/getGroupUsers?now=" + new Date().getTime(), 
                /*使用post方式*/
        		"fnServerData": function ( sSource, aoData, fnCallback ) {
        			var postData = {page:1,start:0,rows:10,params:{}};
        			//参数
        			postData.params.parentId = UITree.selectedId == "" ? "-1" : (UITree.selectedId).toString();
        			for(var i=0,size=aoData.length;i<size;i++){
        				if(aoData[i].name=='iDisplayLength'){
        					postData.rows = aoData[i].value;
        				}else if(aoData[i].name=='iDisplayStart'){
        					postData.start = aoData[i].value;
        				}else if(aoData[i].name=='sEcho'){
        					postData.page = aoData[i].value;
        				}
        			}
                    $.ajax({
                        "dataType": 'json',
                        "cache": false,
                        "type": "POST",
                        "url": sSource,
                        "data": postData,
                        "success": function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
                    });
                },
                "aoColumnDefs": [  
					{  
						"aTargets" : [ 0 ], // ID
						"mRender" : function ( data, type, full ) { 
							return '<input type="checkbox" class="checkbox-item" name="checkbox-item" id="'+ full.id +'" title="'+ full.realName +'">';
						}
					},{  
                      "data": "realName", // 姓名
  		              "aTargets": [ 1 ],
                      'bSortable': true
                    },{  
                    	"data": "userNum",  // 工号
     		        	"aTargets": [ 2 ],
     		        	'bSortable': true
                    },{  
                    	"data": "dimissionStatus", // 是否离职值
                    	"aTargets": [ 3 ]
                    }/*,{  
                    	"data": "department", // 部门
                    	"aTargets": [ 4 ]
                    }*/
                 ]
            });
            datatable = groupUserListTable;
        },
        reSearch : function(){
        	datatable.fnDraw();
        },
        setListener:function(){
        	// 全选
			jQuery("#checkbox-parent").click(function() { // 选择
				var checked = this.checked;
				$(".checkbox-item").each(function() {
					this.checked = checked;
				});
			});
        	// 确认选择
        	jQuery("#chooseGroupUser").click(function(){ // 选择
        		var userNames = "";
        		var userIds = "";
        		userIds += $(".checkbox-item[type=checkbox]:checked").map(function() {
        			userNames += this.title + ",";
		  		   return "'"+this.id+"'";
        		}).get().join(",");
        		if(userNames != "" && userNames.length > 0){
        			userNames = userNames.substring(0, userNames.length-1)
        			$("#userIds").val(userIds);
        			$("#userNames").val(userNames);
        		}
        		fnAddCallback();
        	});
        },
    };

}();